
package com.pp.admin.common.server.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.pp.admin.common.server.PersistenceManagerFactory;

/**
 * The persistent class for the PP_PERMISSIONS database table.
 */
@PersistenceCapable( identityType = IdentityType.APPLICATION )
public class Permission extends Base implements Serializable {
	
	private static final long	serialVersionUID	= 1L;
	
	@PrimaryKey
	@Persistent( valueStrategy = IdGeneratorStrategy.IDENTITY )
	private Key					id;
	
	@Persistent
	private String				description;
	
	@Persistent
	private String				permission;
	
	@Persistent
	private Key					createdBy;
	
	@Persistent
	private Date				createdByTime;
	
	@Persistent
	private Key					lastModefiedBy;
	
	@Persistent
	private Date				lastModefiedByTime;
	
	// bi-directional many-to-one association to Domain
	@Persistent
	private Domain				domain;
	
	@Persistent
	private Set<Key>			roleKeys			= new HashSet<Key>();
	
	public Permission() {

	}
	
	/**
	 * @return the id
	 */
	public Key getId() {

		return id;
	}
	
	/**
	 * @param id
	 *            the id to set
	 */
	public void setId( Key id ) {

		this.id = id;
	}
	
	/**
	 * @return the description
	 */
	public String getDescription() {

		return description;
	}
	
	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription( String description ) {

		this.description = description;
	}
	
	/**
	 * @return the permission
	 */
	public String getPermission() {

		return permission;
	}
	
	/**
	 * @param permission
	 *            the permission to set
	 */
	public void setPermission( String permission ) {

		this.permission = permission;
	}
	
	/**
	 * @return the domain
	 */
	public Domain getDomain() {

		return domain;
	}
	
	/**
	 * @param domain
	 *            the domain to set
	 */
	public void setDomain( Domain domain ) {

		this.domain = domain;
	}
	
	/**
	 * @return the roleKeys
	 */
	public Set<Key> getRoleKeys() {

		return roleKeys;
	}
	
	/**
	 * @param roleKeys
	 *            the roleKeys to set
	 */
	public void setRoleKeys( Set<Key> roleKeys ) {

		this.roleKeys = roleKeys;
	}
	
	/**
	 * @return the createdBy
	 */
	@Override
	public User getCreatedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			user = pm.getObjectById( User.class, this.createdBy );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}
	
	/**
	 * @param createdBy
	 *            the createdBy to set
	 */
	@Override
	public void setCreatedBy( Key createdBy ) {

		this.createdBy = createdBy;
	}
	
	/**
	 * @return the createdByTime
	 */
	@Override
	public Date getCreatedByTime() {

		return createdByTime;
	}
	
	/**
	 * @param createdByTime
	 *            the createdByTime to set
	 */
	@Override
	public void setCreatedByTime( Date createdByTime ) {

		this.createdByTime = createdByTime;
	}
	
	/**
	 * @return the lastModefiedBy
	 */
	@Override
	public User getLastModefiedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			user = pm.getObjectById( User.class, this.lastModefiedBy );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}
	
	/**
	 * @param lastModefiedBy
	 *            the lastModefiedBy to set
	 */
	@Override
	public void setLastModefiedBy( Key lastModefiedBy ) {

		this.lastModefiedBy = lastModefiedBy;
	}
	
	/**
	 * @return the lastModefiedByTime
	 */
	@Override
	public Date getLastModefiedByTime() {

		return lastModefiedByTime;
	}
	
	/**
	 * @param lastModefiedByTime
	 *            the lastModefiedByTime to set
	 */
	@Override
	public void setLastModefiedByTime( Date lastModefiedByTime ) {

		this.lastModefiedByTime = lastModefiedByTime;
	}
}
